From 71ffa3756a8214ac0ded721faa93ae6bbc886abd Mon Sep 17 00:00:00 2001 From: Domas Mituzas Date: Sun, 28 May 2006 18:29:42 +0000 Subject: [PATCH] Allow storing local message cache as PHP source instead of serialized array, this way opcode cache is enjoyed :-) Might be not that secure, so disabled by default. --- includes/DefaultSettings.php | 6 +++ includes/MessageCache.php | 72 +++++++++++++++++++++++++++++++++--- 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index b9b1e10ecc..acfceac2bf 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -547,6 +547,12 @@ $wgMemCachedPersistent = false; * Directory for local copy of message cache, for use in addition to memcached */ $wgLocalMessageCache = false; +/** + * Defines format of local cache + * true - Serialized object + * false - PHP source file (Warning - security risk) + */ +$wgLocalMessageCacheSerialized = true; /** * Directory for compiled constant message array databases diff --git a/includes/MessageCache.php b/includes/MessageCache.php index ea7ae52e09..35ea4d53d5 100644 --- a/includes/MessageCache.php +++ b/includes/MessageCache.php @@ -113,6 +113,50 @@ class MessageCache { @chmod( $filename, 0666 ); } + function loadFromScript( $hash ) { + global $wgLocalMessageCache, $wgDBname; + if ( $wgLocalMessageCache === false ) { + return; + } + + $filename = "$wgLocalMessageCache/messages-$wgDBname"; + + wfSuppressWarnings(); + $file = fopen( $filename, 'r' ); + wfRestoreWarnings(); + if ( !$file ) { + return; + } + $localHash=substr(fread($file,40),8); + fclose($file); + if ($hash!=$localHash) { + return; + } + require("$wgLocalMessageCache/messages-$wgDBname"); + } + + function saveToScript($array, $hash) { + global $wgLocalMessageCache, $wgDBname; + if ( $wgLocalMessageCache === false ) { + return; + } + + $filename = "$wgLocalMessageCache/messages-$wgDBname"; + $oldUmask = umask( 0 ); + wfMkdirParents( $wgLocalMessageCache, 0777 ); + umask( $oldUmask ); + $file = fopen( $filename.'.tmp', 'w'); + fwrite($file,"mCache = array("); + + $re="/(? $message) { + fwrite($file, "'". preg_replace($re, "\'", $key). + "' => '" . preg_replace( $re, "\'", $message) . "',\n"); + } + fwrite($file,");\n?>"); + fclose($file); + rename($filename.'.tmp',$filename); + } /** * Loads messages either from memcached or the database, if not disabled @@ -120,7 +164,7 @@ class MessageCache { * Returns false for a reportable error, true otherwise */ function load() { - global $wgLocalMessageCache; + global $wgLocalMessageCache, $wgLocalMessageCacheSerialized; if ( $this->mDisable ) { static $shownDisabled = false; @@ -141,7 +185,11 @@ class MessageCache { wfProfileIn( $fname.'-fromlocal' ); $hash = $this->mMemc->get( "{$this->mMemcKey}-hash" ); if ( $hash ) { - $this->loadFromLocal( $hash ); + if ($wgLocalMessageCacheSerialized) { + $this->loadFromLocal( $hash ); + } else { + $this->loadFromScript( $hash ); + } } wfProfileOut( $fname.'-fromlocal' ); @@ -157,7 +205,11 @@ class MessageCache { $hash = md5( $serialized ); $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry ); } - $this->saveToLocal( $serialized, $hash ); + if ($wgLocalMessageCacheSerialized) { + $this->saveToLocal( $serialized,$hash ); + } else { + $this->saveToScript( $this->mCache, $hash ); + } } wfProfileOut( $fname.'-fromcache' ); } @@ -188,7 +240,11 @@ class MessageCache { $serialized = serialize( $this->mCache ); $hash = md5( $serialized ); $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry ); - $this->saveToLocal( $serialized, $hash ); + if ($wgLocalMessageCacheSerialized) { + $this->saveToLocal( $serialized,$hash ); + } else { + $this->saveToScript( $this->mCache, $hash ); + } } wfProfileOut( $fname.'-save' ); @@ -289,7 +345,7 @@ class MessageCache { } function replace( $title, $text ) { - global $wgLocalMessageCache, $parserMemc, $wgDBname; + global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc, $wgDBname; $this->lock(); $this->load(); @@ -303,7 +359,11 @@ class MessageCache { $serialized = serialize( $this->mCache ); $hash = md5( $serialized ); $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry ); - $this->saveToLocal( $serialized, $hash ); + if ($wgLocalMessageCacheSerialized) { + $this->saveToLocal( $serialized,$hash ); + } else { + $this->saveToScript( $this->mCache, $hash ); + } } -- 2.20.1